import { google } from 'googleapis';
import log from 'electron-log';

export class GoogleSheetsService {
    private sheets: any;

    constructor() {
        const auth = new google.auth.GoogleAuth({
            keyFile: 'src/plugins/google-sheets/swift-synthesis-450101-j7-08e26bb7334e.json',
            scopes: ['https://www.googleapis.com/auth/spreadsheets'],
        });

        this.sheets = google.sheets({ version: 'v4', auth });
    }

    async readColumnData(spreadsheetId: string, range: string) {
        try {
            const res = await this.sheets.spreadsheets.values.get({
                spreadsheetId,
                range,
            });

            const rows: any[] = res.data.values;
            if (!rows || rows.length === 0) {
                log.info('No data found.');
                return [];
            }

            return rows.map(row => row[0]);
        } catch (error: any) {
            log.error(`Error in readColumnData: ${error.stack || error}`);
            throw error;
        }
    }
}